#!/usr/bin/env ruby
#
# Created by Jerry Luk on 2008-05-01
# Copyright (c) 2008. All rights reserved.

# Represents the Google Data
module RSS  
  module BaseGDataModelWithGCalExtension
    def append_features(klass)
      super
      base_append_features(klass, GDataModelWithGCalExtension::ELEMENT_NAME_INFOS, GDATA_PREFIX, GDATA_URI)
    end
  end
  
  module GDataModelWithGCalExtension
    extend BaseModel
    extend BaseGDataModelWithGCalExtension
  
    TEXT_ELEMENTS = [
      # [name, plural_name, content]
      ["who",           nil,  true],
      ["when",          nil,  false],
      ["recurrence",    nil,  true],
      ["where",         nil,  true],
      ["reminder",      nil,  false],
      ["eventStatus",   nil,  true],
      ["attendeeStatus",nil,  true],
      ["attendeeType",  nil,  true],
      ["visibility",    nil,  true],
      ["transparency",  nil,  true],
      ["originalEvent", nil,  true]
    ]
    
    ATTRIBUTES = GDataModel::ATTRIBUTES + [
      # [name, uri, required, type]
      ["startTime", "", false, [:w3cdtf, :w3cdtf]],
      ["endTime", "", false, [:w3cdtf, :w3cdtf]],
      ["minutes", "", false, :integer],
      ["method", "", false, nil]
    ]
    
    ELEMENT_NAME_INFOS = GDataModelWithGCalExtension::TEXT_ELEMENTS.to_a
    ELEMENTS = TEXT_ELEMENTS.map(&:first)
    
    ELEMENT_NAME_INFOS.each do |name, plural_name, content|
      create_class(name, plural_name, GDATA_PREFIX, GDATA_URI, content)
    end    
  end
    
  GDataModelWithGCalExtension::ELEMENTS.each do |name|
    class_name = Utils.to_class_name_with_prefix(name, GDATA_PREFIX)
    BaseListener.install_class_name(GDATA_URI, name, class_name)
  end
  
  GDataModelWithGCalExtension::ELEMENTS.collect! { |name| "#{GDATA_PREFIX}_#{name}"}
end

module RSS
  module Atom
    class Feed
      include GDataModelWithGCalExtension
      class Entry; include GDataModelWithGCalExtension; end
    end

    class Entry
      include GDataModelWithGCalExtension
    end
  end
  
  module GDataModelWithGCalExtension
    class GdWhen < Element
      install_have_child_element("reminder", GDATA_URI, "*", "gd_reminder")
      class GdReminder < GDataModelWithGCalExtension::GdReminder
        @tag_name = "reminder"
      end
    end
    class GdOriginalEvent < Element
      install_have_child_element("when", GDATA_URI, "*", "gd_when")
      class GdWhen < GDataModelWithGCalExtension::GdWhen
        @tag_name = "when"
      end
    end
  end
end
